<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="ts_code" label="" required>
        <uni-easyinput placeholder="TS代码 (000001.SZ)" v-model="formData.ts_code"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="symbol" label="" required>
        <uni-easyinput placeholder="股票代码 (000001)" v-model="formData.symbol"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="name" label="" required>
        <uni-easyinput placeholder="股票名称" v-model="formData.name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="fullname" label="">
        <uni-easyinput placeholder="股票全称" v-model="formData.fullname"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="enname" label="">
        <uni-easyinput placeholder="英文名称" v-model="formData.enname"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cnspell" label="">
        <uni-easyinput placeholder="拼音缩写" v-model="formData.cnspell"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="area" label="">
        <uni-easyinput placeholder="所在地域" v-model="formData.area"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="industry" label="">
        <uni-easyinput placeholder="所属行业" v-model="formData.industry"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="market" label="">
        <uni-easyinput placeholder="市场类型（主板/创业板/科创板）" v-model="formData.market"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="exchange" label="">
        <uni-easyinput placeholder="交易所 (SSE/SZSE)" v-model="formData.exchange"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="curr_type" label="">
        <uni-easyinput placeholder="交易货币" v-model="formData.curr_type"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="list_status" label="">
        <uni-data-checkbox v-model="formData.list_status" :localdata="formOptions.list_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="list_date" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.list_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="is_hs" label="">
        <uni-easyinput placeholder="是否沪深港通标的，N否 H沪股通 S深股通" v-model="formData.is_hs"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="act_name" label="">
        <uni-easyinput placeholder="实际控制人名称" v-model="formData.act_name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="act_ent_type" label="">
        <uni-easyinput placeholder="实际控制人企业性质" v-model="formData.act_ent_type"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="logo" label="">
        <uni-easyinput placeholder="公司logo URL" v-model="formData.logo"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="description" label="">
        <uni-easyinput placeholder="公司简介" v-model="formData.description"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="patent_count" label="">
        <uni-easyinput placeholder="专利总量" type="number" v-model="formData.patent_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="update_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.update_time"></uni-datetime-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/a_stocks.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'a_stocks';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "ts_code": "",
        "symbol": "",
        "name": "",
        "fullname": "",
        "enname": "",
        "cnspell": "",
        "area": "",
        "industry": "",
        "market": "",
        "exchange": "",
        "curr_type": "",
        "list_status": "",
        "list_date": null,
        "is_hs": "",
        "act_name": "",
        "act_ent_type": "",
        "logo": "",
        "description": "",
        "patent_count": null,
        "create_time": null,
        "update_time": null
      }
      return {
        formData,
        formOptions: {
          "list_status_localdata": [
            {
              "value": "L",
              "text": "L"
            },
            {
              "value": "D",
              "text": "D"
            },
            {
              "value": "P",
              "text": "P"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("ts_code,symbol,name,fullname,enname,cnspell,area,industry,market,exchange,curr_type,list_status,list_date,is_hs,act_name,act_ent_type,logo,description,patent_count,create_time,update_time").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
